﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class UserControls_ProfessionalMembershipUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private bool ValidateFormFields(string mode)
    {
        bool hasFilledAllRequiredFields = true;

        if (mode == "UPDATE")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(ProfessionalMembership_ObjectDataSource.UpdateParameters["institution"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(ProfessionalMembership_ObjectDataSource.UpdateParameters["certification"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(ProfessionalMembership_ObjectDataSource.UpdateParameters["date_attained"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(ProfessionalMembership_ObjectDataSource.UpdateParameters["position_held"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
        }

        if (mode == "INSERT")
        {

            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(ProfessionalMembership_ObjectDataSource.InsertParameters["institution"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(ProfessionalMembership_ObjectDataSource.InsertParameters["certification"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(ProfessionalMembership_ObjectDataSource.InsertParameters["date_attained"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(ProfessionalMembership_ObjectDataSource.InsertParameters["position_held"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
        }

        return hasFilledAllRequiredFields;
    }

    protected void rgProfessionalMembership_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.UpdateCommandName)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            ProfessionalMembership_ObjectDataSource.UpdateParameters["profMembershipKey"].DefaultValue = SRM.Help.GetHiddenFieldValue(item.FindControl("profMembershipKey"));
            ProfessionalMembership_ObjectDataSource.UpdateParameters["institution"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("institution_textbox"));
            ProfessionalMembership_ObjectDataSource.UpdateParameters["certification"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("certification_textbox"));
            ProfessionalMembership_ObjectDataSource.UpdateParameters["date_attained"].DefaultValue = SRM.Help.GetRadDatePickerValue(item.FindControl("date_attained_datepicker"));
            ProfessionalMembership_ObjectDataSource.UpdateParameters["position_held"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("position_help_textbox"));
            
            if (ValidateFormFields("UPDATE"))
            {
                ProfessionalMembership_ObjectDataSource.Update();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("ProfessionalMembershipMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
        }

        if (e.CommandName == RadGrid.DeleteCommandName)
        {
            GridDataItem item = e.Item as GridDataItem;
            ProfessionalMembership_ObjectDataSource.DeleteParameters["profMembershipKey"].DefaultValue = item["ProfMembershipKey_column"].Text;
            ProfessionalMembership_ObjectDataSource.Delete();
        }

        if (e.CommandName == RadGrid.PerformInsertCommandName)
        {
            GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
            ProfessionalMembership_ObjectDataSource.InsertParameters["institution"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("institution_textbox"));
            ProfessionalMembership_ObjectDataSource.InsertParameters["certification"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("certification_textbox"));
            ProfessionalMembership_ObjectDataSource.InsertParameters["date_attained"].DefaultValue = SRM.Help.GetRadDatePickerValue(item.FindControl("date_attained_datepicker"));
            ProfessionalMembership_ObjectDataSource.InsertParameters["position_held"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("position_help_textbox"));

            if (ValidateFormFields("INSERT"))
            {
                ProfessionalMembership_ObjectDataSource.Insert();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("ProfessionalMembershipMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }

        }
    }
}